package cn.nextProject.controller;

import cn.nextProject.entity.Result;
import cn.nextProject.entity.User;
import cn.nextProject.mapper.UserMapper;
import cn.nextProject.service.LoginService;
import cn.nextProject.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user
     * @return
     * @throws SQLException
     * @throws IOException
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user) throws SQLException, IOException {

        if (ObjectUtils.isEmpty(user)){
            return new Result<>().fail("用户名或密码不能为空");
        }

        String username = user.getUsername();
        String password = user.getUserPassword();

        if (ObjectUtils.isEmpty(username)){
            return new Result<>().fail("用户名不能为空");
        }
        if (ObjectUtils.isEmpty(password)){
            return new Result<>().fail("密码不能为空");
        }

        Map<String, Object> login = loginService.login(user);

        if (CollectionUtils.isEmpty(login)){
            return new Result<>().fail("系统错误");
        }

        if ((boolean) login.get("state")){
            return new Result<>().successLogin("登录成功",login.get("user"), (String) login.get("token"));
        }else{
            return new Result<>().fail((String) login.get("msg"));
        }

    }

    /**
     * 注册
     * @param user
     * @return
     * @throws SQLException
     * @throws IOException
     */
    @PostMapping("/register")
    public Result<?> register(@RequestBody User user) throws SQLException, IOException {

        if (ObjectUtils.isEmpty(user)){
            return new Result<>().fail("用户名或密码不能为空");
        }

        String username = user.getUsername();
        String password = user.getUserPassword();
        String realname = user.getRealname();
        String idCard = user.getIdCard();


        if (ObjectUtils.isEmpty(username)){
            return new Result<>().fail("用户名不能为空");
        }

        User user1 = userService.getUserByUsernameNoPassword(username);
        if (!ObjectUtils.isEmpty(user1)){
            return new Result<>().fail("用户名已存在");
        }

        if (ObjectUtils.isEmpty(password)){
            return new Result<>().fail("密码不能为空");
        }
        if (ObjectUtils.isEmpty(realname)){
            return new Result<>().fail("真实姓名不能为空");
        }
        if (ObjectUtils.isEmpty(idCard)){
            return new Result<>().fail("身份证号不能为空");
        }

        Map<Integer, String> register = loginService.register(user);

        if (CollectionUtils.isEmpty(register)){
            return new Result<>().fail("注册失败");
        }

        if (register.get(0) != null){
            return new Result<>().fail(register.get(0));
        }

        if (register.get(1) != null){
            return new Result<>().success("注册成功",register.get(1));
        }

        return new Result<>().fail("注册失败");
    }

}
